<?php
/**
 * テストフォームコントローラー
 *
 * @package  app
 * @extends  Controller
 */
class Controller_Manage_Econfirm extends Controller {
	/**
	 * index
	 *
	 * @access  public
	 * @return  Response
	 */
        public $member_id = '';
	public function action_index() {
                
                $get = Input::get();
                $view = array();
                if(!isset($get['member_id'])|| $get['member_id']=='')
                   throw new HttpNotFoundException;
               
               // echo $get['member_id'];
              // $member_where_info = array('member_status' => 0, 'member_id' => $member_id );
                $query = DB::select('member_id');
		$query -> from('member_info');
                $query->where('member_id',$get['member_id']);
               // $query->where('member_status'=>0);
                $result = $query -> execute() -> as_array();
                if(count($result)== 0)
                     throw new HttpNotFoundException;
                $view['member_id'] =  $get['member_id'];
                $view['uuid'] ='update';
                $view['message'] ='';
		return Response::forge(View::forge('manage/econfirm/index',$view), D_HTTP_UPDATE);
                
	}
        
        public function action_regist()
        {
                $get = Input::get();
                $view = array();
                die();
                if(!isset($get['member_id'])|| $get['member_id']=='')
                   throw new HttpNotFoundException;
                $query = DB::select('*');
		$query -> from('member_info');
                $query->where('member_id',$get['member_id']);
               // $query->where('member_status','0');
                $result = $query -> execute() -> as_array();
                if(count($result)==0)
                     throw new HttpNotFoundException;
                $view['member_id'] =  $get['member_id'];
                $view['uuid'] ='regist';
                $view['message'] ='';
		return Response::forge(View::forge('manage/econfirm/index',$view), D_HTTP_UPDATE);
        }

        /**
	 * api
	 *
	 * @access  public
	 * @return  Response
	 */
        
	public function  action_cf()
        {
            
            $post = Input::post();
            $view = array();
            if(!isset($post['member_id'])||$post['member_id']=='')
                throw new HttpNotFoundException;
            $view['member_id'] = $post['member_id'];
            if(!isset($post['em'])||$post['em'] == '')
            {
                $view['message'] = 'please input an email';
                $view['error'] = 'error';
                return Response::forge(View::forge('manage/econfirm/index',$view), D_HTTP_UPDATE);
            }
            
            else {
                
               
                if(!filter_var($post['em'], FILTER_VALIDATE_EMAIL)){
                     $view['message'] = 'Invalid email, please input another email';
                     return Response::forge(View::forge('manage/econfirm/index',$view), D_HTTP_UPDATE);
                }
               //save email.
                
               else {
                        
                        $query = DB::select('member_id','point_has');
			$query -> from('member_info');
                        $query->where('member_email',$post['em']);
			$result = $query -> execute() -> as_array();
                        
                        if(count($result)==0)
                        {
                            $query = DB::update('member_info');
                            $query -> set(array("member_email" =>$post['em'] ));
			    $query -> where('member_id', $post['member_id']);
			    $query -> execute();
                            $view['message']='申請が完了しました';
                            return Response::forge(View::forge('manage/econfirm/success',$view), D_HTTP_UPDATE);
                           // header("Location: Commesodo://");
                           // exit();
                                
                        }else
                        {
                            if($post['uuid']=='update')
                            {
                                //update point here 
                                 $query = DB::update('member_info');
                                 $query -> set(array("member_email" =>$post['em'],'point_has'=> DB::expr("point_has+" . $result[0]['point_has'])));
                                 $query -> where('member_id', $post['member_id']);
                                 $query -> execute();
                                 
                                 // delete mail and point of old user 
                                 $query = DB::update('member_info');
                                 $query -> set(array("member_email" =>'',"point_has" => '0'));
                                 $query -> where('member_id', $result[0]['member_id']);
                                 $query -> execute();
                                 $view['message'] =' 引き継ぎ完了しました';//
                                  return Response::forge(View::forge('manage/econfirm/success',$view), D_HTTP_UPDATE);
                                 //header("Location: Commesodo://");
                                 
                                 //exit();
                            }  else {
                          
                                //case 1 
                                if($result[0]['member_id']=$post['member_id'])
                                     $view['message'] = 'updated sucessfull!' ;
                                
                                $view['message'] = 'This email is already existing, please input another email';
                                
                            }
                            //update point 
                    
                         }
                            
               }
            }
                       
          
            return Response::forge(View::forge('manage/econfirm/success',$view), D_HTTP_UPDATE);
        }
        
        public function  action_econfirmstatic()
        {
            $view = array();
            $query = DB::select(DB::expr(' count(member_email) AS tt '));
            $query ->from('member_info');
            $query ->where('member_email','<>','');
            $result = $query->execute()->as_array();
           // echo $result[0]['tt'];
            $view['total'] = $result[0]['tt'];
            return Response::forge(View::forge('manage/econfirm/econfirmstastic',$view), D_HTTP_UPDATE);
        }

}
